#include "Linklist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"
#include <stdint.h>
#include "driver/uart.h"
#include "driver/gpio.h"
#include "RTC.h"
#include "spiffs.h"
char* send_str1 = "finger.t4.txt=\"error ID!!!\"\xff\xff\xff";
char* send_str2 = "finger.t4.txt=\" \"\xff\xff\xff";

char* send_name_head = "finger.t3.txt=\"";
char* send_name_tail = "\"\xff\xff\xff";

char* send_position_head = "finger.t6.txt=\"";
char* send_position_tail = "\"\xff\xff\xff";

char* send_head_name = "sign.t1.txt=\"";
char* send_tail_name = "\"\xff\xff\xff";

char* send_head_position = "sign.t2.txt=\"";
char* send_tail_position = "\"\xff\xff\xff";

char* send_head_ID = "sign.t3.txt=\"";
char* send_tail_ID = "\"\xff\xff\xff";

char* send_name_log = "log.t6.txt=\"";
char* send_card_status_1 = "log.t7.txt=\"";
char* send_card_status_2 = "log.t21.txt=\"";
char* send_card_status_3 = "log.t22.txt=\"";

uint8_t administrator[] = {0xB9,0xDC,0xC0,0xED,0xD4,0xB1};
uint8_t user[] = {0xD3,0xC3,0xBB,0xA7};
uint8_t card_succ[] = {0xC7,0xA9,0xB5,0xBD,0xB3,0xC9,0xB9,0xA6};
uint8_t card_late[] = {0xB3,0xD9,0xB5,0xBD};
uint8_t card_none[] = {0xCE,0xB4,0xC7,0xA9,0xB5,0xBD};

uint8_t compare_arrays(const uint8_t* arr1, const uint8_t* arr2, size_t size) 
{
    size_t i = 0;
    for (i = 0; i < size; i++) 
    {
        if (arr1[i] > arr2[i]) 
        {
            return 2;  // arr1 较大
        } 
        else if (arr1[i] < arr2[i]) 
        {
            return 1; // arr2 较大
        }
    }
    return 0; // 数组相等
}


void Personal_Information_create(char* name,uint16_t ID,uint8_t position,uint8_t name_size,Infomation_t* previous)
{
    Infomation_t* Ptail = previous;
    uint8_t i = 0;
    while(Ptail->next != NULL)
    {
        Ptail = Ptail->next;
    }
    Infomation_t* current = (Infomation_t*)malloc(sizeof(Infomation_t));
    Ptail->next = current;
    Ptail = Ptail->next;
    memcpy(current->name,name,20);
    current->ID = ID;
    current->position = position;
    current->name_size = name_size;
    current->param = 0;
    for(i = 0;i < 90;i++)
    {
        current->card_time[i] = 0;
        current->status[i] = 0;
    }
    Ptail->next = NULL;
}

void Personal_Information_delete(uint16_t ID,Infomation_t* Head)
{
    Infomation_t* current = Head;
    Infomation_t* prev = Head;
    if(current->ID != ID && current->next != NULL)
    {
        current = current->next;
    }
    while(current->ID != ID && current->next != NULL)
    {
        current = current->next;
        prev = prev->next;
    }
    if(current->next == NULL && current->ID != ID)
    {
        ESP_LOGI("uart","数据库中无此指纹的人");
        uart_write_bytes(SCREEN_UART,send_str1,strlen(send_str1));
    }
    else
    {
        current = current->next;
        prev->next = current;
    }
}

void Personal_Information_Search(uint16_t ID,Infomation_t* previous,time_t now_get,struct tm* timeinfo_get_param,Card_clock_t* set_time,uint8_t card_model)
{
    Infomation_t* current = previous;
    while(current->ID != ID && current->next != NULL)
    {
        current = current->next;
    }
    if(current->next == NULL && current->ID != ID)
    {
        ESP_LOGI("uart","数据库中无此指纹的人");
    }
    else
    {
        uart_write_bytes(SCREEN_UART,send_head_name,strlen(send_head_name));
        uart_write_bytes(SCREEN_UART,current->name,current->name_size);
        uart_write_bytes(SCREEN_UART,send_tail_name,strlen(send_tail_name));
        vTaskDelay(pdMS_TO_TICKS(200));
        if(current->position == 0)
        {
            uart_write_bytes(SCREEN_UART,send_head_position,strlen(send_head_position));
            uart_write_bytes(SCREEN_UART,administrator,sizeof(administrator));
            uart_write_bytes(SCREEN_UART,send_tail_position,strlen(send_tail_position));

        }
        else if(current->position == 1)
        {
            uart_write_bytes(SCREEN_UART,send_head_position,strlen(send_head_position));
            uart_write_bytes(SCREEN_UART,user,sizeof(user));
            uart_write_bytes(SCREEN_UART,send_tail_position,strlen(send_tail_position));
    
        }
        uint8_t i = 0;
        time(&now_get);  // 获取当前时间

        localtime_r(&now_get,timeinfo_get_param);  // 将time_t格式的时间转换为tm结构体格式
        if(current->param == 89)
            current->param = 0;
        i = current->param;
        current->card_time[i] = now_get; //将当前时间存到链表

        uint8_t time[2] = {'0','0'};
        if(card_model == ONCE)
        {
            time[0] = '0' + timeinfo_get_param->tm_mday / 10;
            time[1] = '0' + timeinfo_get_param->tm_mday % 10;
            if(compare_arrays(time,set_time->date,2) == 1)
            {
                current->status[i] = CARD_SUCCESS;
            }
            else if(compare_arrays(time,set_time->date,2) == 0)
            {
                time[0] = '0' + timeinfo_get_param->tm_hour / 10;
                time[1] = '0' + timeinfo_get_param->tm_hour % 10;
                if(compare_arrays(time,set_time->hour,2) == 1)
                {
                    current->status[i] = CARD_SUCCESS;
                }
                else if(compare_arrays(time,set_time->hour,2) == 0)
                {
                    time[0] = '0' + timeinfo_get_param->tm_min / 10;
                    time[1] = '0' + timeinfo_get_param->tm_min % 10;
                    if(compare_arrays(time,set_time->min,2) == 1)
                    {
                        current->status[i] = CARD_SUCCESS;
                    }
                    else if(compare_arrays(time,set_time->min,2) == 0)
                    {
                        time[0] = '0' + timeinfo_get_param->tm_sec / 10;
                        time[1] = '0' + timeinfo_get_param->tm_sec % 10;
                        if(compare_arrays(time,set_time->sec,2) == 1)
                        {
                            current->status[i] = CARD_SUCCESS;
                        }
                        else if(compare_arrays(time,set_time->sec,2) == 0)
                        {
                            current->status[i] = CARD_SUCCESS;
                        }
                        else if(compare_arrays(time,set_time->sec,2) == 2)
                        {
                            current->status[i] = CARD_LATE;
                        }
                    }
                    else if(compare_arrays(time,set_time->min,2) == 2)
                    {
                        if(time[0] - set_time->min[0] > 2)
                        {
                            current->status[i] = CARD_NONE;
                        }
                        else
                        {
                            current->status[i] = CARD_LATE;
                        }
                    }
                }
                else if(compare_arrays(time,set_time->hour,2) == 2)
                {
                    current->status[i] = CARD_NONE;
                }
            }
            else if(compare_arrays(time,set_time->date,2) == 2)
            {
                current->status[i] = CARD_NONE;
            }
        }
        else if(card_model == EVERY_DAY)
        {
            time[0] = '0' + timeinfo_get_param->tm_hour / 10;
            time[1] = '0' + timeinfo_get_param->tm_hour % 10;   
            if(timeinfo_get_param->tm_hour - ((set_time->hour[0] - '0') * 10 + (set_time->hour[1] - '0')) >= 1)
            {
                set_time++;
                if(timeinfo_get_param->tm_hour - ((set_time->hour[0] - '0') * 10 + (set_time->hour[1] - '0')) >= 1)
                {
                    set_time++;
                }
            }

            if(compare_arrays(time,set_time->hour,2) == 1)
            {
                current->status[i] = CARD_SUCCESS;
            }
            else if(compare_arrays(time,set_time->hour,2) == 0)
            {
                time[0] = '0' + timeinfo_get_param->tm_min / 10;
                time[1] = '0' + timeinfo_get_param->tm_min % 10;
                if(compare_arrays(time,set_time->min,2) == 1)
                {
                    current->status[i] = CARD_SUCCESS;
                }
                else if(compare_arrays(time,set_time->min,2) == 0)
                {
                    time[0] = '0' + timeinfo_get_param->tm_sec / 10;
                    time[1] = '0' + timeinfo_get_param->tm_sec % 10;
                    if(compare_arrays(time,set_time->sec,2) == 1)
                    {
                        current->status[i] = CARD_SUCCESS;
                    }
                    else if(compare_arrays(time,set_time->sec,2) == 0)
                    {
                        current->status[i] = CARD_SUCCESS;
                    }
                    else if(compare_arrays(time,set_time->sec,2) == 2)
                    {
                        current->status[i] = CARD_LATE;
                    }
                }
                else if(compare_arrays(time,set_time->min,2) == 2)
                {
                    if(time[0] - set_time->min[0] > 2)
                    {
                        current->status[i] = CARD_NONE;
                    }
                    else
                    {
                        current->status[i] = CARD_LATE;
                    }
                }
            }
            else if(compare_arrays(time,set_time->hour,2) == 2)
            {
                current->status[i] = CARD_NONE;
            }   
        }
        else if(card_model == WORK_DAY)
        {
            time[0] = '0' + timeinfo_get_param->tm_hour / 10;
            time[1] = '0' + timeinfo_get_param->tm_hour % 10;   
            if(timeinfo_get_param->tm_hour - ((set_time->hour[0] - '0') * 10 + (set_time->hour[1] - '0')) >= 1)
            {
                set_time++;
                if(timeinfo_get_param->tm_hour - ((set_time->hour[0] - '0') * 10 + (set_time->hour[1] - '0')) >= 1)
                {
                    set_time++;
                }
            }

            if(compare_arrays(time,set_time->hour,2) == 1)
            {
                current->status[i] = CARD_SUCCESS;
            }
            else if(compare_arrays(time,set_time->hour,2) == 0)
            {
                time[0] = '0' + timeinfo_get_param->tm_min / 10;
                time[1] = '0' + timeinfo_get_param->tm_min % 10;
                if(compare_arrays(time,set_time->min,2) == 1)
                {
                    current->status[i] = CARD_SUCCESS;
                }
                else if(compare_arrays(time,set_time->min,2) == 0)
                {
                    time[0] = '0' + timeinfo_get_param->tm_sec / 10;
                    time[1] = '0' + timeinfo_get_param->tm_sec % 10;
                    if(compare_arrays(time,set_time->sec,2) == 1)
                    {
                        current->status[i] = CARD_SUCCESS;
                    }
                    else if(compare_arrays(time,set_time->sec,2) == 0)
                    {
                        current->status[i] = CARD_SUCCESS;
                    }
                    else if(compare_arrays(time,set_time->sec,2) == 2)
                    {
                        current->status[i] = CARD_LATE;
                    }
                }
                else if(compare_arrays(time,set_time->min,2) == 2)
                {
                    if(time[0] - set_time->min[0] > 2)
                    {
                        current->status[i] = CARD_NONE;
                    }
                    else
                    {
                        current->status[i] = CARD_LATE;
                    }
                }
            }
            else if(compare_arrays(time,set_time->hour,2) == 2)
            {
                current->status[i] = CARD_NONE;
            }
        }
        current->param++;
    }
}

void Personal_Information_Search_manager(uint16_t ID,Infomation_t* previous)
{
    Infomation_t* current = previous;
    while(current->ID != ID && current->next != NULL)
    {
        current = current->next;
    }
    if(current->next == NULL && current->ID != ID)
    {
        ESP_LOGI("uart","数据库中无此指纹的人");
        uart_write_bytes(SCREEN_UART,send_str1,strlen(send_str1));
    }
    else
    {
        uart_write_bytes(SCREEN_UART,send_name_head,strlen(send_name_head));
        uart_write_bytes(SCREEN_UART,current->name,current->name_size);
        uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
        vTaskDelay(pdMS_TO_TICKS(200));
        uart_write_bytes(SCREEN_UART,send_str2,strlen(send_str2));
        vTaskDelay(pdMS_TO_TICKS(200));
        if(current->position == 0)
        {
            uart_write_bytes(SCREEN_UART,send_position_head,strlen(send_position_head));
            uart_write_bytes(SCREEN_UART,administrator,sizeof(administrator));
            uart_write_bytes(SCREEN_UART,send_position_tail,strlen(send_position_tail));
            gpio_set_level(LED_GPIO,0);

        }
        else if(current->position == 1)
        {
            uart_write_bytes(SCREEN_UART,send_position_head,strlen(send_position_head));
            uart_write_bytes(SCREEN_UART,user,sizeof(user));
            uart_write_bytes(SCREEN_UART,send_position_tail,strlen(send_position_tail));
        }
    }
}


void Personal_Information_Search_Log(uint16_t ID,Infomation_t* previous,uint8_t page_num)
{
    Infomation_t* current = previous;
    while(current->ID != ID && current->next != NULL)
    {
        current = current->next;
    }
    if(current->next == NULL && current->ID != ID)
    {
        ESP_LOGI("uart","数据库中无此指纹的人");
    
    }
    else
    {
        uart_write_bytes(SCREEN_UART,send_name_log,strlen(send_name_log));
        uart_write_bytes(SCREEN_UART,current->name,current->name_size);
        uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));

        //第一行
            if(current->status[page_num * 3] == CARD_SUCCESS)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_1,strlen(send_card_status_1));
                uart_write_bytes(SCREEN_UART,card_succ,sizeof(card_succ));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            else if(current->status[page_num * 3] == CARD_LATE)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_1,strlen(send_card_status_1));
                uart_write_bytes(SCREEN_UART,card_late,sizeof(card_late));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            else if(current->status[page_num * 3] == CARD_NONE)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_1,strlen(send_card_status_1));
                uart_write_bytes(SCREEN_UART,card_none,sizeof(card_none));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }

            //第二行
            if(current->status[page_num * 3 + 1] == CARD_SUCCESS)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_2,strlen(send_card_status_2));
                uart_write_bytes(SCREEN_UART,card_succ,sizeof(card_succ));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            else if(current->status[page_num * 3 + 1] == CARD_LATE)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_2,strlen(send_card_status_2));
                uart_write_bytes(SCREEN_UART,card_late,sizeof(card_late));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            else if(current->status[page_num * 3 + 1] == CARD_NONE)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_2,strlen(send_card_status_2));
                uart_write_bytes(SCREEN_UART,card_none,sizeof(card_none));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            //第三行
            if(current->status[page_num * 3 + 2] == CARD_SUCCESS)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_3,strlen(send_card_status_3));
                uart_write_bytes(SCREEN_UART,card_succ,sizeof(card_succ));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            else if(current->status[page_num * 3 + 2] == CARD_LATE)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_3,strlen(send_card_status_3));
                uart_write_bytes(SCREEN_UART,card_late,sizeof(card_late));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }
            else if(current->status[page_num * 3 + 2] == CARD_NONE)
            {
                uart_write_bytes(SCREEN_UART,send_card_status_3,strlen(send_card_status_3));
                uart_write_bytes(SCREEN_UART,card_none,sizeof(card_none));
                uart_write_bytes(SCREEN_UART,send_name_tail,strlen(send_name_tail));
            }

        Show_Personal_Log(current->card_time,page_num);
    }
}
